package com.sbp.message.api.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * 该类的唯一作用是 获取 配置中的属性, 可以static方法获得
 * */
public class MessageSysInfoService {
    private static final Logger logger = LoggerFactory.getLogger(MessageSysInfoService.class);

    private static String appName;
    public static String getAppName() {
        return MessageSysInfoService.appName;
    }
    public void setAppName(String appName) {
        MessageSysInfoService.appName = appName;
    }


    private static String companyName;
    public static String getCompanyName() {
        return MessageSysInfoService.companyName;
    }
    public void setCompanyName(String companyName) {
        MessageSysInfoService.companyName = companyName;
    }

    private static String env;
    public static String getEnv() {
        return MessageSysInfoService.env;
    }
    public void setEnv(String env) {
        MessageSysInfoService.env = env;
    }


    public static String generateKafkaRealTopicName(String sourceTopicName) {
        String topicEnvName;
        if ("product".equals(env)) {
            topicEnvName = env;
        } else {
            topicEnvName = getDetailEnvName();
            if (StringUtils.isEmpty(topicEnvName)) {
                topicEnvName = env;
            }
        }

        return companyName + "_" + topicEnvName + "-" + sourceTopicName.replace('.', '-');
    }

    public static String generateKafkaRealGroupId(String group) {
        if ("product".equals(env)) {
            return group;
        } else {
            return getDetailEnvName() + "-" + group;
        }
    }

    private static String detailEnvName = null;
    private static String getDetailEnvName() {
        if (null != detailEnvName) {
            return detailEnvName;
        } else {
            detailEnvName = System.getenv("ENV_NAME");
            logger.warn("detailEnvName={}", detailEnvName);
            return detailEnvName;
        }
    }
}
